package com.cloud.note.repository;

import com.cloud.note.model.Attachment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface AttachmentRepository extends JpaRepository<Attachment, String> {
    List<Attachment> findByNoteId(String noteId);

    @Query("SELECT a FROM Attachment a JOIN a.note n WHERE a.id = :id AND n.userId = :userId")
    Optional<Attachment> findByIdAndUserId(@Param("id") String id, @Param("userId") String userId);

    @Query("SELECT a FROM Attachment a JOIN a.note n WHERE a.noteId = :noteId AND n.userId = :userId")
    List<Attachment> findByNoteIdAndUserId(@Param("noteId") String noteId, @Param("userId") String userId);
}